const express = require('express')
const app = express()

app.get('/', async (req, res, next) => {
  try {
    console.log(err)
    res.send('get /')
  } catch (error) {
    next(error)
  }
})

app.get('/foo', async (req, res, next) => {
  try {
    res.send('get /foo')
  } catch (error) {
    next(error)
  }
})

// 错误处理中间件（在所有的中间件之后挂）
app.use((err, req, res, next) => {
  // 4个参数缺一不可
  // console.log('错误', err)
  res.status(500).json({
    error: err.message
  })
})
app.listen(3000, () => {
  console.log(`Server running at http://localhost:3000/`)
})
